// DAO/Impl/AssignmentDaoImpl.java
package com.hp.DAO.Impl;                         // 实现包

import com.hp.DAO.AssignmentDao;                 // 接口：AssignmentDao
import com.hp.DAO.BaseDao;                       // 基类
import com.hp.Model.Assignment;                  // 实体：作业
import com.hp.Util.RowMapper;                    // 行映射器
import com.hp.Util.SimpleJdbcTemplate;           // JDBC 模板

import java.sql.Timestamp;                // 时间戳
import java.util.Date;                    // Date
import java.util.List;                    // List

/** AssignmentDao 的 JDBC 实现 */
public class AssignmentDaoImpl extends BaseDao implements AssignmentDao {
    public AssignmentDaoImpl(SimpleJdbcTemplate jdbc) { super(jdbc); } // 构造注入

    // RowMapper：ResultSet -> Assignment
    private static final RowMapper<Assignment> M = rs -> {
        Assignment a = new Assignment();                                // 新建实体
        a.setId(rs.getLong("id"));                                      // id
        a.setCourseId(rs.getLong("course_id"));                         // 课程ID
        a.setCreatedBy(rs.getLong("created_by"));                       // 创建者
        a.setTitle(rs.getString("title"));                              // 标题
        a.setDescription(rs.getString("description"));                  // 描述
        Timestamp due = rs.getTimestamp("due_at");                      // 截止时间
        Timestamp c = rs.getTimestamp("created_at");                    // 创建时间
        Timestamp u = rs.getTimestamp("updated_at");                    // 更新时间
        a.setDueAt(due == null ? null : new Date(due.getTime()));       // 转 Date
        a.setMaxPoints(rs.getInt("max_points"));                        // 分值
        a.setAllowResubmit(rs.getBoolean("allow_resubmit"));            // 允许重交
        a.setCreatedAt(c == null ? null : new Date(c.getTime()));       // 转 Date
        a.setUpdatedAt(u == null ? null : new Date(u.getTime()));       // 转 Date
        return a;                                                       // 返回
    };

    @Override
    public Assignment findById(Long id) {                               // 按主键查询
        String sql = "SELECT id,course_id,created_by,title,description,due_at,max_points,allow_resubmit,created_at,updated_at " +
                "FROM assignment WHERE id=?";                       // SQL
        return queryOne(sql, M, id);                                     // 执行查询
    }

    @Override
    public List<Assignment> listByCourse(Long courseId, int offset, int limit) { // 按课程分页
        String sql = "SELECT id,course_id,created_by,title,description,due_at,max_points,allow_resubmit,created_at,updated_at " +
                "FROM assignment WHERE course_id=? ORDER BY id DESC LIMIT ? OFFSET ?"; // SQL
        return queryList(sql, M, courseId, limit, offset);               // 执行查询
    }

    @Override
    public Long insert(Assignment a) {                                   // 新增作业
        String sql = "INSERT INTO assignment (course_id,created_by,title,description,due_at,max_points,allow_resubmit,created_at,updated_at) " +
                "VALUES (?,?,?,?,?,?,?,?,?)";                        // SQL
        Date now = new Date();                                           // 当前时间
        Timestamp due = a.getDueAt() == null ? null : new Timestamp(a.getDueAt().getTime()); // 允许为空
        Timestamp c = a.getCreatedAt() == null ? new Timestamp(now.getTime()) : new Timestamp(a.getCreatedAt().getTime()); // 创建时间
        Timestamp u = a.getUpdatedAt() == null ? new Timestamp(now.getTime()) : new Timestamp(a.getUpdatedAt().getTime()); // 更新时间
        return execInsertReturnId(sql, a.getCourseId(), a.getCreatedBy(), a.getTitle(), a.getDescription(),
                due, a.getMaxPoints(), a.getAllowResubmit(), c, u);      // 执行插入
    }

    @Override
    public int updateBasic(Assignment a) {                               // 更新基础字段
        String sql = "UPDATE assignment SET title=?, description=?, due_at=?, max_points=?, allow_resubmit=?, updated_at=? WHERE id=?"; // SQL
        Timestamp due = a.getDueAt() == null ? null : new Timestamp(a.getDueAt().getTime()); // 截止时间
        Timestamp u = a.getUpdatedAt() == null ? new Timestamp(System.currentTimeMillis())
                : new Timestamp(a.getUpdatedAt().getTime()); // 更新时间
        return execUpdate(sql, a.getTitle(), a.getDescription(), due, a.getMaxPoints(), a.getAllowResubmit(), u, a.getId()); // 执行更新
    }

    @Override
    public int deleteById(Long id) {                                     // 删除
        String sql = "DELETE FROM assignment WHERE id=?";                // SQL
        return execUpdate(sql, id);                                      // 执行
    }
}
